<template>
  <view>
    <signature :showCanvas="showCanvas" @finish="finish"></signature>
  </view>
</template>

<script>
import signature from "@/components/signature.vue";
import axios from "axios";
import * as consts from "@/utils/constant.js";
export default {
  data() {
    return {
      //打开canvas绘制签名
      showCanvas: true,
      inductionId:''
    };
  },
  components: {
    signature,
  },
  onLoad(option){
    console.log("inductionId==222==>",option.inductionId)
    this.inductionId = option.inductionId
  },
  mounted() {},
  methods: {
    async finish(base64String) {
      const service = axios.create({
        baseURL: consts.API_BASE_URL,
        timeout: 1000 * 60 * 3,
        headers: {
          "Content-Type": "multipart/form-data",
        },
      });
      service.interceptors.request.use(
        (config) => {
          if (
            config.url.indexOf("withoutLogin") == -1 ||
            config.url.indexOf("bindAndLogin") == -1 ||
            config.url.indexOf("listDept") == -1
          ) {
            let token = uni.getStorageSync("token");
            token && token !== "" && (config.headers.Authorization = "Bearer " + token);
          }
          return config;
        },
        (error) => {
          return Promise.reject(error);
        }
      );

      //这里对base64串进行操作，去掉url头，并转换为byte
      let bytes = window.atob(base64String.split(",")[1]);
      let array = [];
      for (let i = 0; i < bytes.length; i++) {
        array.push(bytes.charCodeAt(i));
      }
      let blob = new Blob([new Uint8Array(array)], { type: "image/png" });
      let formData = new FormData();
      formData.append("file", blob, Date.now() + ".png");

      service({
        url: "/xsdcrm-wechat-api/wxseparateAgreement/signUpload",
        method: "POST",
        data: formData,
      }).then(async (respon) => {
        let res = respon.data;
        if (res.code != 200) {
          uni.showToast({
            title: res.msg,
            icon: "error",
          });
          return;
        }
        uni.showLoading({
          title: "正在签署协议，请稍后···",
        });

        const fl = new FormData();
        fl.append("signPath", res.fileName);
        fl.append("inductionId", this.inductionId);
        // const fl = {
        //   signPath:res.fileName,
        //   inductionId:this.inductionId
        // }
        service({
          url: "/xsdcrm-wechat-api/riders/contract/signAgreement",
          method: "POST",
          data: fl,
        }).then(async (res) => {
          uni.hideLoading();
          let { data } = res;
          let { code, msg } = data;
          if (code != 200) {
            uni.showToast({
              title: msg,
              icon: "error",
            });
            setTimeout(() => {
              uni.setStorageSync("refresh", "1");
              this.$u.route("pages/pdf/webIndex2?inductionId="+this.inductionId);
            }, 1000);
            return;
          }
          uni.showToast({
            title: "协议签署成功！",
            icon: "success",
          });
          setTimeout(() => {
            this.showCanvas = false;
            this.$store.commit("UPDATE_SIGN_STATUS", true);
            uni.setStorageSync("refresh", "1");
            this.$u.route("pages/pdf/webIndex2?inductionId="+this.inductionId);
          }, 300);
        });
      });
    },
  },
};
</script>

<style lang="scss" scoped>
uni-page-body,
uni-view {
  height: 100%;
}
.iframe {
  height: 85vh;
  overflow-y: scroll;
}
.custom-btn {
  position: absolute;
  bottom: 10px;
  margin: 15px auto;
  left: 50%;
  transform: translateX(-50%);
}
.loading-box {
  position: fixed;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  text-align: center;

  .text {
    margin-top: 10px;
  }
}
</style>
